/*
 * Copyright (c) 2025, 廖树财, <study_2030@163.com>
 *
 * PES1.0.3-YCYW is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2     
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
#pragma once

#if defined(WIN32) || defined(WIN64) || defined(_MSC_VER)
#include "windows.h"
#else
#include <unistd.h>
#endif

#include <stdio.h>
#include <string.h>
#include <time.h>
#include "sqlite3.h"
#include "cQueue.h"
#include <stdbool.h>
#include <syslog.h>

typedef struct stru_MODEL_CTL
{
    int deviceFlag;         // 终端
    int virtualizationFlag; // 容器
    int appFlag;            // 应用
    int securityFlag;       // 告警
    int diagnosticsFlag;    // 诊断
    int safeFlag;           // 安全
    int _4gModules;         // 4G模块使能开关
    int appModules;         // 微应用appm使能开关
    int web;                // http服务
    int third;
    int plc;                //载波网管
} ST_MODEL_CTL;

typedef struct stru_ST_CLIENT_INFO
{
    char* server_addr; // Need to obtain the server_addr of the device from the platform side
    int port;          // Need to obtain the port of the device from the platform side
    char* device_id;   // Need to obtain the device_id of the device from the platform side
    char* user_name;   // Need to obtain the device_id of the device from the platform side
    char* passwd;      // Need to obtain the password of the device from the platform side
    char* client_id;   // Need to obtain the device_id of the device from the platform side
    char* service_id;
    char* manufacturerId;
    char* manufacturerName;
    char* model;
    char* prefix;
    int SSL;
    int sencrypt;
    int is_ipv6;
    char* device_type;
    // char* g_workPath = ".";
    int keepAlivetime; /* Set the heartbeat interval to 120 seconds */
    int scram;
                       // char query_message[128];
} ST_CLIENT_INFO;

typedef struct
{
    int status; // 参数1
    ST_CLIENT_INFO clientInfo;
    // MQTT登录配置
    void (*setLoginConfig)();
    // 设置全局回调函数，包括终端管理，日志管理...
    void (*setCallbacks)(ST_MODEL_CTL st_model_ctl);
    // MQTT登录处理，包含客户端创建、连接以及对应的回调函数注册。
    int (*Login)();
    // MQTT线程管理
    void (*pthread_manage)();
    // MQTT 命令行消息队列
    cQueue_t* cmdPq;
    // MQTT 网络测试消息队列
    cQueue_t* netPq;
} ob_context;

#define CQUEUE_MAX 1024

#define HW_TRUE 1  /**< Indicates HW_TRUE */
#define HW_FALSE 0 /**< Indicates HW_FALSE */

#define HW_SUCCESS 0        /**< Indicates success */
#define HW_FAILED 1         /**< Indicates failed */
#define HW_NULL ((void *)0) /**< Indicates null ptr */

typedef int HW_INT;           /**< Indicates type of int. */
typedef unsigned int HW_UINT; /**< Indicates type of unsigned int. */
// typedef char HW_CHAR;           /**< Indicates type of char. */
typedef unsigned char HW_UCHAR; /**< Indicates type of unsigned char. */
typedef int HW_BOOL;            /**< Indicates type of HW_BOOL. */
typedef void HW_VOID;           /**< Indicates type of void. */

#define IOTA_SUCCESS 0
#define IOTA_FAILURE -1
#define IOTA_PARAMETER_EMPTY -101
#define IOTA_RESOURCE_NOT_AVAILABLE -102
#define IOTA_INITIALIZATION_REPEATED -103
#define IOTA_LIBRARY_LOAD_FAILED -104
#define IOTA_SECRET_ENCRYPT_FAILED -105
#define IOTA_MQTT_CONNECT_FAILED -106
#define IOTA_MQTT_CONNECT_EXISTED -107
#define IOTA_CERTIFICATE_NOT_FOUND -108

#define IOTA_TOPIC_PREFIX "/v1/devices/"
#define IOTA_TOPIC_POSTFIX_TOPO_ADD "/topo/add"
#define IOTA_TOPIC_POSTFIX_TOPO_ADDRESPONSE "/topo/addResponse"
#define IOTA_TOPIC_POSTFIX_TOPO_UPDATE "/topo/update"
#define IOTA_TOPIC_POSTFIX_TOPO_UPDATERESPONSE "/topo/updateResponse"
#define IOTA_TOPIC_POSTFIX_TOPO_DELETE "/topo/delete"
#define IOTA_TOPIC_POSTFIX_TOPO_QUERY "/topo/query"
#define IOTA_TOPIC_POSTFIX_TOPO_QUERYRESPONSE "/topo/queryResponse"
#define IOTA_TOPIC_POSTFIX_COMMAND "/command"
#define IOTA_TOPIC_POSTFIX_COMMANDRESPONSE "/commandResponse"
#define IOTA_TOPIC_POSTFIX_DATAS "/datas"

#define EagleEyeSMG_INI_DIR "/PowerHarmony/SMC/app/EagleEyeSMG/conf/EagleEyeSMG.ini"
#define EagleEyeSMG_MQTT_INI_DIR "/PowerHarmony/SMC/app/EagleEyeSMG/conf/mqtt.ini"

typedef enum enum_IOTA_CALLBACK_SETTING
{
    EN_IOTA_CALLBACK_CONNECT_SUCCESS = 11,
    EN_IOTA_CALLBACK_CONNECT_FAILURE = 12,
    EN_IOTA_CALLBACK_DISCONNECT_SUCCESS = 13,
    EN_IOTA_CALLBACK_DISCONNECT_FAILURE = 14,
    EN_IOTA_CALLBACK_CONNECTION_LOST = 15,

    EN_IOTA_CALLBACK_PUBLISH_SUCCESS = 21,
    EN_IOTA_CALLBACK_PUBLISH_FAILURE = 22,

    EN_IOTA_CALLBACK_SUBSCRIBE_SUCCESS = 31,
    EN_IOTA_CALLBACK_SUBSCRIBE_FAILURE = 32,

    EN_IOTA_CALLBACK_COMMAND_ARRIVED = 41,

    EN_IOTA_CALLBACK_DEVICE_ADDITION_RESULT = 51,
    EN_IOTA_CALLBACK_DEVICE_DELETION_RESULT = 52,
    EN_IOTA_CALLBACK_DEVICE_UPDATE_RESULT = 53,
    EN_IOTA_CALLBACK_DEVICE_QUERY_RESULT = 54,

    EN_IOTA_CALLBACK_EXTERNAL_INTERFACE1 = 60,

} EN_IOTA_CALLBACK_SETTING;

typedef enum enum_IOTA_CFG_TYPE
{
    EN_IOTA_CFG_DEVICEID = 0, // the value for this item is deviceId or nodeId
    EN_IOTA_CFG_DEVICESECRET = 1,
    EN_IOTA_CFG_USERNAME = 2,
    EN_IOTA_CFG_CLIENTID = 3,

    EN_IOTA_CFG_MQTT_ADDR = 5,
    EN_IOTA_CFG_MQTT_PORT = 6,
    EN_IOTA_CFG_MQTT_URL_PREFIX = 7,

    EN_IOTA_CFG_AUTH_MODE = 9, // for configuration value of this item, please refer to @enum_IOTA_CFG_AUTH_MODE below

    EN_IOTA_CFG_LOG_LOCAL_NUMBER = 10, // take effect only when syslog is available
    EN_IOTA_CFG_LOG_LEVEL = 11,        // take effect only when syslog is available

    EN_IOTA_CFG_KEEP_ALIVE_TIME = 12,
    EN_IOTA_CFG_CONNECT_TIMEOUT = 13,
    EN_IOTA_CFG_RETRY_INTERVAL = 14,

    EN_IOTA_CFG_RESET_SECRET_IN_PROGRESS = 15,

    EN_IOTA_CFG_QOS = 16,
    EN_IOTA_CFG_SSL = 17,
} EN_IOTA_CFG_TYPE;

typedef enum enum_IOTA_CFG_AUTH_MODE
{
    EN_IOTA_CFG_AUTH_MODE_DEVICE_ID = 0,
    EN_IOTA_CFG_AUTH_MODE_NODE_ID = 2,
} ENUM_IOTA_CFG_AUTH_MODE;

typedef enum enum_IOTA_CFG_URL_PREFIX
{
    EN_IOTA_CFG_URL_PREFIX_TCP = 0,
    EN_IOTA_CFG_URL_PREFIX_SSL = 1,
} ENUM_IOTA_CFG_URL_PREFIX;

typedef enum enum_LOG_LEVEL
{
    EN_LOG_LEVEL_DEBUG = 0,
    EN_LOG_LEVEL_INFO = 1,
    EN_LOG_LEVEL_WARNING = 2,
    EN_LOG_LEVEL_ERROR = 3,

} LOGLEVEL;

typedef struct stru_ST_IOTA_DEVICE_INFO
{
    char* pcNodeId;
    char* pcDeviceId;
    char* pcName;
    char* pcDescription;
    char* pcManufacturerId;
    char* pcManufacturerName;
    char* pcModel;
} ST_IOTA_DEVICE_INFO;

typedef struct stru_ST_IOTA_ADD_DEVICE_RSP
{
    int iStatusCode;
    char* pcStatusDecs;
    ST_IOTA_DEVICE_INFO stDeviceInfos;
} ST_IOTA_ADD_DEVICE_RSP;

typedef struct stru_ST_IOTA_BatchReportProperties
{
    char* pcDeviceId;
    char* pcServiceId;
    char* pcPayload;
} ST_IOTA_BatchReportProperties;

typedef struct stru_ST_IOTA_DEVICE_STATUS
{
    char* pcDeviceId;
    char* pcStatus;
} ST_IOTA_DEVICE_STATUS;

typedef struct stru_ST_IOTA_UPDATE_STATUS_RSP
{
    int iStatusCode;
    char* pcStatusDecs;
    char* pcDeviceId;
} ST_IOTA_UPDATE_STATUS_RSP;

typedef struct stru_ST_IOTA_COMMAND
{
    char* pcDeviceId;
    char* pcMsgType;
    char* pcServiceId;
    char* pcCmd;
    char* pcParas; // ObjectNode, json string
    int iMid;
} ST_IOTA_COMMAND;

typedef struct stru_ST_IOTA_COMMAND_RSP
{
    char* pcMsgType;
    int iMid;
    int iErrorCode;
    char* pcBody; // ObjectNode, json string
} ST_IOTA_COMMAND_RSP;

typedef struct stru_ST_IOTA_SERVICES
{
    char* pcServiceId;
    char* pcData; // ObjectNode, json string
    char* pcEventTime;
} ST_IOTA_SERVICES;

typedef struct stru_ST_IOTA_LOG_PARAM
{
    char* logPath;
    char* beginTime;
    char* endTime;
} ST_IOTA_LOG_PARAM;

typedef struct stru_ST_IOTA_LOG
{
    char* iMid;
    char* Servicename;
    char* cmd;
    void* param;
    char* zone;
    char* node;
    char* deviceSn;
    char* requestID;
    char* msgTime;
    char* deviceID;
} ST_IOTA_LOG;

typedef struct stru_ST_IOTA_LOG_BODY
{
    char* FileName;
    char* filed;
    char* sha256;
    char* md5;
    long size;
} ST_IOTA_LOG_BODY;

typedef struct stru_ST_IOTA_LOG_RES
{
    char* iMid;
    char* replyId;
    int code;
    void* body;
    char* zone;
    char* node;
    char* deviceSn;
    char* requestID;
    char* msgTime;
    char* deviceID;
    int deviceType;
    char *subDeviceId;
} ST_IOTA_LOG_RES;

typedef struct stru_ST_IOTA_LOG_RULE_PARAM
{
    char* path;
    char* regex;
} ST_IOTA_LOG_RULE_PARAM;

typedef struct stru_ST_IOTA_DEVICE_DATA
{
    char* user;
    char* srcIp;
    HW_UINT* srcPort;
    char* dstIp;
    HW_UINT* dstPort;
    char* loginTime;
    char* opTime;
    char* cmd;
    char* cmdOutput;
} ST_IOTA_DEVICE_DATA;

typedef struct stru_ST_IOTA_DEVICE_SERVICES
{
    char* service;
    char* mid;
    char* eventTime;
    ST_IOTA_DEVICE_DATA data;
} ST_IOTA_DEVICE_SERVICES;

typedef struct stru_ST_IOTA_DEVICE_LOG
{
    char* deviceSn;
    ST_IOTA_DEVICE_SERVICES services[];
} ST_IOTA_DEVICE_LOG;

typedef struct stru_ST_IOTA_ALARM_LOG
{
    char* reqNo;
    char* deviceSn;
    char* deviceId;
    char* bussActTime;
    char* logActTime;
    char* bussId;
    char* bussName;
    char* srvName;
    char* moduleId;
    char* moduleName;
    char* logType;
    char* logTypeName;
    char* operUserId;
    char* operUserName;
    char* bussRetCode;
    char* bussRetMsg;
    char* className;
    char* methodName;
    char* orgId;
    char* orgName;
    char* clientIP;
    char* srvIP;
    char* zone;
    char* zoneName;
    char* node;
    char* nodeName;
    char* globalEventId;
    char* logInfo;
    char* domain;
    char* domainName;
    char* url;
    char* bussType;
    char* extendInfo;
} ST_IOTA_ALARM_LOG;

typedef struct stru_ST_IOTA_LOGIN_RES
{
    char* access_token;
    char* refresh_token;
    char* scope;
    char* token_type;
    char* short_token;
    int expires_in;
    char* client_id;
} ST_IOTA_LOGIN_RES;

typedef struct stru_ST_IOTA_AUTH_RES_V1
{
    char* token;
    char* timestamp;
    char* nonce;
} ST_IOTA_AUTH_RES_V1;

typedef struct stru_ST_IOTA_LOGIN_RES_V1
{
    int resultCode;
    char* message;
    ST_IOTA_AUTH_RES_V1* data;
    long timeStamp;
} ST_IOTA_LOGIN_RES_V1;

typedef struct stru_ST_IOTA_AUTH_RES
{
    char* contentType;
    char* accessKey;
    char* timestamp;
    char* nonce;
    char* sign;
    char* url;
} ST_IOTA_AUTH_RES;

typedef struct stru_ST_IOTA_FILE_RES
{
    int* resultCode;
    char* message;
    char* data;
} ST_IOTA_FILE_RES;

typedef struct stru_ST_IOTA_FILE_DATA
{
    char* fileId;
    int fileSize;
    char* zone;
    char* node;
} ST_IOTA_FILE_DATA;

typedef struct stru_ST_IOTA_REPORT_LOG_DATA
{
    char* globalEventId;
    char* reqNo;
    char* deviceSn;
    char* deviceId;
    char* bussActTime;
    char* logActTime;
    char* bussId;
    char* bussName;
    char* srvName;
    char* moduleId;
    char* moduleName;
    char* logType;
    char* logTypeName;
    char* operUserId;
    char* operUserName;
    char* bussRetCode;
    char* bussRetMsg;
    char* className;
    char* methodName;
    char* orgId;
    char* orgName;
    char* clientIP;
    char* srvIP;
    char* zone;
    char* node;
    char* nodeName;
    char* logInfo;
    char* domain;
    char* bussType;
} ST_IOTA_REPORT_LOG_DATA;

typedef struct stru_IOTA_SYSDATA
{
    char* user;
    char* srcIp;
    int srcPort;
    char* dstIp;
    int dstPort;
    char* loginTime;
    char* opTime;
    char* cmd;
    char* cmdOutput;

} IOTA_SYSDATA;

typedef struct stru_IOTA_SYSSVR
{
    char* service;
    char* mid;
    IOTA_SYSDATA data;
    char* eventTime;
} IOTA_SYSSVR;

typedef struct stru_IOTA_SYSLOG
{
    char* deviceSn;
    IOTA_SYSSVR services;
} IOTA_SYSLOG;

typedef struct stru_ST_IOTA_WEAKPASSWD
{
    int code;
    char* service;
    char* zone;
    char* node;
    char* deviceSn;
    char* deviceId;
    char* msgTime;
    void* params;
} ST_IOTA_WEAKPASSWD;

typedef struct stru_IOTA_FILE_CONF_V1
{
    int resultCode;
    char* message;

    char* appId;
    char* appKey;
    char* uploadFileUrl;
    char* downloadFileUrl;
} IOTA_FILE_CONF_V1;

typedef struct st_IOTA_NETWORKLIST_PARAM_V1
{
    char operation[8];
    char protocol[8];
    char srcIp[16];
    char srcPort[64];
    char dstIp[16];
    char dstPort[64];
    char permissionStatus[16];
    char policyId[128];
    int priority;
} IOTA_NETWORKLIST_PARAM_V1, * PIOTA_NETWORKLIST_PARAM_V1;

